package com.ly.hotel.util;

import java.lang.ref.WeakReference;

import com.ly.hotel.R;
import com.ly.hotel.view.common.BaseActivity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ToastUtil {
	private static Toast mToast;

	private static ThisHandler mHandler = new ThisHandler(null);

	private static class ThisHandler extends Handler {
		private WeakReference<ToastUtil> wrActivity;

		public ThisHandler(BaseActivity activity) {
			wrActivity = new WeakReference<ToastUtil>(null);
		}

		@Override
		public void handleMessage(Message message) {
			ToastUtil act = wrActivity.get();
			if (act != null) {
				switch (message.what) {
				case 1:
					mToast.cancel();
					mToast = null;// toast隐藏后，将其置为null

					break;

				}
			}
		}
	}

	public static void showShortToast(Context context, String message, int time) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.my_toast, null);// 自定义布局
		TextView text = (TextView) view.findViewById(R.id.toast_message);// 显示的提示文字
		text.setText(message);
		if (mToast == null) {// 只有mToast==null时才重新创建，否则只需更改提示文字
			mToast = new Toast(context);
		}
		mToast.setDuration(Toast.LENGTH_LONG);
		mToast.setGravity(Gravity.CENTER, 0, 0);
		mToast.setView(view);
		if(mHandler.hasMessages(1)){
			mHandler.removeMessages(1);
		}
		mHandler.sendEmptyMessageDelayed(1, time);
		mToast.show();
//		execToast(mToast,time/1000);
	}
	
	public static void showShortToastCoustom(Context context, String message, int time, int wOffset) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.my_toast, null);// 自定义布局
		view.setAlpha(50);
		TextView text = (TextView) view.findViewById(R.id.toast_message);// 显示的提示文字
		text.setText(message);
		if (mToast == null) {// 只有mToast==null时才重新创建，否则只需更改提示文字
			mToast = new Toast(context);
		}
		mToast.setDuration(Toast.LENGTH_SHORT);
		mToast.setGravity(Gravity.CENTER, wOffset, 0);
		mToast.setView(view);
		if(mHandler.hasMessages(1)){
			mHandler.removeMessages(1);
		}
		mHandler.sendEmptyMessageDelayed(1, time);
		mToast.show();
//		execToast(mToast,time/1000);
	}
/*	
	private static void showMyToast(Toast toast, int cnt) {
		if (cnt < 0)
			return;
		toast.show();
		execToast(toast, cnt);
	}
	
	private static void execToast(final Toast toast, final int cnt) {
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				showMyToast(toast, cnt - 1);
			}
		}, 2000);
	}*/
	
}
